/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.blacklist.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.blacklist.vo.ClosedBlacklistCarVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.close.blacklist.entity.ClosedBlacklistCarEntity;
import com.hopes.bizz.close.blacklist.dto.ClosedBlacklistCarExcelDTO;
import com.hopes.bizz.close.blacklist.vo.ClosedBlacklistCarExcelVO;
import com.hopes.bizz.close.blacklist.service.ClosedBlacklistCarService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 车辆黑名单
 *
 * @author pig
 * @date 2024-01-29 18:40:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/blacklist/car" )
@Tag(description = "car" , name = "车辆黑名单管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedBlacklistCarController {

    private final ClosedBlacklistCarService closedBlacklistCarService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param closedBlacklistCarVO 车辆黑名单
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('blacklist_car_view')" )
    public R getClosedBlacklistCarPage(Page page, ClosedBlacklistCarVO closedBlacklistCarVO) {
        LambdaQueryWrapper<ClosedBlacklistCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(closedBlacklistCarVO.getType()), ClosedBlacklistCarEntity::getType, closedBlacklistCarVO.getType());
        wrapper.eq(ObjUtil.isNotNull(closedBlacklistCarVO.getOrgId()), ClosedBlacklistCarEntity::getOrgId, closedBlacklistCarVO.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(closedBlacklistCarVO.getSyncFlag()), ClosedBlacklistCarEntity::getSyncFlag, closedBlacklistCarVO.getSyncFlag());
        wrapper.like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getLicencePlateNum, closedBlacklistCarVO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getDriverName, closedBlacklistCarVO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getDriverPhone, closedBlacklistCarVO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getDriverIdNum, closedBlacklistCarVO.getKeyword());
        return R.ok(closedBlacklistCarService.page(page, wrapper));
    }


    /**
     * 通过id查询车辆黑名单
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('blacklist_car_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(closedBlacklistCarService.getById(id));
    }

    /**
     * 新增车辆黑名单
     * @param closedBlacklistCar 车辆黑名单
     * @return R
     */
    @Operation(summary = "新增车辆黑名单" , description = "新增车辆黑名单" )
    @SysLog("新增车辆黑名单" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('blacklist_car_add')" )
    public R save(@RequestBody ClosedBlacklistCarEntity closedBlacklistCar) {
        return R.ok(closedBlacklistCarService.save(closedBlacklistCar));
    }

    /**
     * 修改车辆黑名单
     * @param closedBlacklistCar 车辆黑名单
     * @return R
     */
    @Operation(summary = "修改车辆黑名单" , description = "修改车辆黑名单" )
    @SysLog("修改车辆黑名单" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('blacklist_car_edit')" )
    public R updateById(@RequestBody ClosedBlacklistCarEntity closedBlacklistCar) {
        return R.ok(closedBlacklistCarService.updateById(closedBlacklistCar));
    }

    /**
     * 通过id删除车辆黑名单
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除车辆黑名单" , description = "通过id删除车辆黑名单" )
    @SysLog("通过id删除车辆黑名单" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('blacklist_car_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedBlacklistCarService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('blacklist_car_export')")
    public List<ClosedBlacklistCarExcelVO> template() {
        return closedBlacklistCarService.exportExcel(Collections.singletonList(new ClosedBlacklistCarEntity()));
    }

    /**
     * 导出excel 表格
     * @param closedBlacklistCarVO 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('blacklist_car_export')")
    public List<ClosedBlacklistCarExcelVO> export( ClosedBlacklistCarVO closedBlacklistCarVO) {
        LambdaQueryWrapper<ClosedBlacklistCarEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(closedBlacklistCarVO.getType()), ClosedBlacklistCarEntity::getType, closedBlacklistCarVO.getType());
        wrapper.eq(ObjUtil.isNotNull(closedBlacklistCarVO.getOrgId()), ClosedBlacklistCarEntity::getOrgId, closedBlacklistCarVO.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(closedBlacklistCarVO.getSyncFlag()), ClosedBlacklistCarEntity::getSyncFlag, closedBlacklistCarVO.getSyncFlag());
        wrapper.like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getLicencePlateNum, closedBlacklistCarVO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getDriverName, closedBlacklistCarVO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getDriverPhone, closedBlacklistCarVO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedBlacklistCarVO.getKeyword()), ClosedBlacklistCarEntity::getDriverIdNum, closedBlacklistCarVO.getKeyword());
        return closedBlacklistCarService.exportExcel(closedBlacklistCarService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param closedBlacklistCarExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('blacklist_car_export')")
    public R importExcel(@RequestExcel List<ClosedBlacklistCarExcelDTO> closedBlacklistCarExcelDtoList, BindingResult bindingResult) {
        return closedBlacklistCarService.importExcel(closedBlacklistCarExcelDtoList, bindingResult);
    }
}
